package com.gadgets.framework.dict.controller;

import com.gadgets.framework.common.constant.GadgetsExceptionConstant;
import com.gadgets.framework.common.dto.DictQueryDTO;
import com.gadgets.framework.common.dto.HttpBaseResp;
import com.gadgets.framework.common.exception.GadgetsServiceException;
import com.gadgets.framework.common.pojo.Dict;
import com.gadgets.framework.common.util.CacheUtils;
import com.gadgets.framework.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiujunqing
 * @version V1.0.1
 * @data 2021-09-22
 * @moduel com.gadgets.framework.dict.controller
 */
@RestController
@RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/dict")
public class DictController {

    @Autowired
    private CacheUtils cacheUtils;

    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/forgeDictGet.do")
    @SuppressWarnings("unchecked")
    public HashMap<String, HashMap<String,String>> queryDict(HttpServletRequest request, HttpServletResponse response, DictQueryDTO dictQueryDTO){
        if(null==dictQueryDTO.getGroupCodeArr()){
            throw new GadgetsServiceException(GadgetsExceptionConstant.VALIDATE_ERROR,"字典码值不能为空");
        }
        HashMap<String, HashMap<String,String>> resultContainer = new HashMap<>();
        for (String dictGroupCode : dictQueryDTO.getGroupCodeArr()) {
            resultContainer.put(dictGroupCode,cacheUtils.getDictParamCache(dictGroupCode));
        }
        return resultContainer;
    }

    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/getDictList.do")
    @SuppressWarnings("unchecked")
    public HttpBaseResp<Dict> queryDictList(HttpServletRequest request, HttpServletResponse response, DictQueryDTO dictQueryDTO){
        if(null==dictQueryDTO.getGroupCodeArr()){
            throw new GadgetsServiceException(GadgetsExceptionConstant.VALIDATE_ERROR,"字典码值不能为空");
        }
        List<Dict> dictList = new ArrayList<>();
        for (String dictGroupCode : dictQueryDTO.getGroupCodeArr()) {
            HashMap<String ,String> dictMap = cacheUtils.getDictParamCache(dictGroupCode);
            boolean Nofilter = StringUtils.isBlank(dictQueryDTO.getKey());
            for (Map.Entry<String, String> dictEntry : dictMap.entrySet()) {
                if(Nofilter || dictEntry.getKey().contains(dictQueryDTO.getKey())) {
                    Dict dict = new Dict();
                    dict.setKey(dictEntry.getKey());
                    dict.setValue(dictEntry.getValue());
                    dict.setGroupCode(dictGroupCode);
                    dictList.add(dict);
                }
            }
        }
        return new HttpBaseResp<>(dictList,null);
    }

}
